package me.zhengjie.modules.management.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.management.domain.ConferenceType;
import me.zhengjie.modules.management.domain.MrNotice;
import me.zhengjie.modules.management.domain.Pageable;
import me.zhengjie.modules.management.mapper.MrNoticeMapper;
import me.zhengjie.modules.management.service.MrNoticeService;
import me.zhengjie.utils.PageHelpResultUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @website https://docs.auauz.net
 * @Description:
 * @Auther: chao.ji
 * @Date: 2020/12/15
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MrNoticeServiceImp implements MrNoticeService {

    private final MrNoticeMapper mrNoticeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNotice(MrNotice mrNotice) {
        try{
            mrNotice.setNotice(mrNotice.getNotice());
            mrNotice.setCreateDateTime(new Timestamp(new Date().getTime()));
            mrNotice.setCreateUserId(SecurityUtils.getCurrentUsername());
            mrNotice.setModifyDateTime(new Timestamp(new Date().getTime()));
            mrNotice.setModifyUserId(SecurityUtils.getCurrentUsername());
            mrNoticeMapper.addNotice(mrNotice);
        }catch(Exception e){
            throw new BadRequestException("新增操作失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNotice(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                mrNoticeMapper.deleteNotice(id);
            }
        } catch (RuntimeException e) {
            throw new BadRequestException("删除操作失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNotice(MrNotice mrNotice) {
        try {
            mrNotice.setNotice(mrNotice.getNotice());
            mrNotice.setModifyDateTime(new Timestamp(new Date().getTime()));
            mrNotice.setModifyUserId(SecurityUtils.getCurrentUsername());
            mrNoticeMapper.updateNotice(mrNotice);
        } catch (RuntimeException e) {
            throw new BadRequestException("更新操作失败！");
        }
    }

    @Override
    public Map<String, Object> queryNotice(Pageable pageable) {
        PageHelper.startPage(pageable.getPage(), pageable.getSize());
        List<MrNotice> list=mrNoticeMapper.queryNotice();
        PageInfo<ConferenceType> pageInfo = new PageInfo(list);
        return PageHelpResultUtil.toPage(pageInfo);
    }


}
